Hello everybody, i have a string with different words in it. They are separated with comma and return. Like this: word1, word2, word3
Now i wrote a function to separate the words. This function writes the single letters from the words with put in an array. (put(newarray, oldstring[i], i,k)) When one word is complete i add an '\0' to terminate the string. And the i need to list the word in a list. I tried it with insert(mylist, index, (string get(newarray, i, k))). But then i get an error. Any ideas?
dxlinformatik - Tue Feb 11 12:05:41 EST 2014 |
Re: Problem with manual created strings On the first read it seemed to me you really need some more "string-fu" (string knowledge) in DXL so I wanted to suggest you to read the string/buffer documentation of DXL again. On the second read of the post, I asked myself "what is he trying to do" ... Anyway: Separating a string is kind of a solved problem and you will find examples on the forum. Normally as a result of splitting up a string, you will have a Skip list of strings (or an array of strings), but never an array of chars. And manually inserting null characters in a string in DXL is also a bad idea. So what exactly are trying to achieve? Split up a string by comma and insert them into a list? Here is an example for splitting a string with commas and newlines and putting them to a skip and a join example for reading them back from the Skip (you might instead want to insert the separate strings into the list). You might also want to read about regular expressions.
Skip splitByComma (string s, int count, Skip sk) {
int pos = 0, iLen = length s;
while (pos < iLen) {
if (s[pos] == ',') {
put(sk, count, s[0:pos-1]);
while (isspace(s[pos+1])) pos++ // comment this in to cut the newlines
splitByComma(s[pos+1:], count + 1, sk);
return sk;
}
pos++
}
put(sk, count, s);
return sk;
}
Skip splitByComma(string s) { return splitByComma(s, 0, create()) }
string join (Skip sk, string sep) {
Buffer joinBuf = create();
int count = 0;
string s; for s in sk do { if (count++ != 0) joinBuf += sep; joinBuf += s }
string sResult = stringOf joinBuf;
delete joinBuf;
return sResult;
}
print join (splitByComma(","), "#") "\n"
print join (splitByComma("Hallo"), "#") "\n"
print join (splitByComma("Hallo,\n Welt"), "#") "\n"
print join (splitByComma("Hallo, Welt"), "#") "\n"
Maybe this helps, regards, Mathias |